package com.myapp.actions.client;

import com.myapp.entity.Client;
import com.myapp.entity.Sex;
import com.myweb.WebAction;
import com.myweb.WebException;
import com.myweb.WebRequest;
import com.myweb.WebResponse;
import javax.persistence.EntityManager;

public class AddClient implements WebAction {

  private Client client = new Client();

  @Override
  public void prepare(WebRequest request) throws WebException {
    client.setFirstName(request.getStringNN("firstName"));
    client.setMiddleName(request.getString("middleName"));
    client.setLastName(request.getStringNN("lastName"));
    client.setBirthdate(request.getDateNN("birthdate", "yyyy-MM-dd"));
    client.setSex(Sex.valueOf(request.getStringNN("sex").toUpperCase()));
    client.setContactInfo(request.getString("contactInfo"));
    client.setPastMedicalHistory(request.getString("pastMedicalHistory"));
    client.setPastSurgicalHistory(request.getString("pastSurgicalHistory"));
    client.setAllergies(request.getString("allergies"));
    client.setFamilyHistory(request.getString("familyHistory"));
    client.setSocialHistory(request.getString("socialHistory"));
    client.setDeleted(false);
  }

  @Override
  public EntityManager process(WebRequest req, WebResponse res, EntityManager em) throws Exception {
    em.persist(client);
    res.setMessage("Successfully added client");
    res.setResult(client);
    return em;
  }
}
